import { reqcartdelete, reqcartedit, reqcartlist } from "../../http/api";
import { Toast } from "antd-mobile";

//初始值
const initState = {
  normalList: [],
  groupList: [],
  allChecked: false,
};

//types
const TYPES = {
  SHOP_CHANGE_NORMALLIST: "SHOP_CHANGE_NORMALLIST",
  SHOP_CHANGE_GROUPLIST: "SHOP_CHANGE_GROUPLIST",
  SHOP_CHANGE_ALLCHECKED: "SHOP_CHANGE_ALLCHECKED",
};

//修改state的reducer
const reducer = (state = initState, action) => {
  switch (action.type) {
    case TYPES.SHOP_CHANGE_NORMALLIST:
      return {
        ...state,
        normalList: action.normalList,
      };
    case TYPES.SHOP_CHANGE_GROUPLIST:
      return {
        ...state,
        groupList: action.groupList,
      };
    case TYPES.SHOP_CHANGE_ALLCHECKED:
      return {
        ...state,
        allChecked: action.allChecked,
      };
    default:
      return state;
  }
};

//actionCreators
export const actions = {
  changeNormalList: (normalList) => ({
    type: TYPES.SHOP_CHANGE_NORMALLIST,
    normalList,
  }),
  changeGroupList: (groupList) => ({
    type: TYPES.SHOP_CHANGE_GROUPLIST,
    groupList,
  }),
  changeAllChecked: (allChecked) => ({
    type: TYPES.SHOP_CHANGE_ALLCHECKED,
    allChecked,
  }),

  //获取购物车列表数据
  getList: () => {
    return (dispatch, getState) => {
      let uid = JSON.parse(sessionStorage.getItem("userInfo")).uid;
      let {
        shop: { normalList, groupList },
      } = getState();
      reqcartlist({ uid }).then((res) => {
        if (res.data.code == 200) {
          //添加复选框属性
          res.data.list[0].content.forEach((item) => {
            //判断是否已经有这条数据，如果有，把本身自己的值赋给，如果没有，给false
            let obj = groupList.find((i) => i.id == item.id);
            item.checked = obj ? obj.checked : false;
            // item.checked = false;
          });
          res.data.list[1].content.forEach((item) => {
            // item.checked = false;
            let obj = normalList.find((i) => i.id == item.id);
            item.checked = obj ? obj.checked : false;
          });
          dispatch(
            actions.changeNormalList(
              res.data.list[1].content ? res.data.list[1].content : []
            )
          );
          dispatch(
            actions.changeGroupList(
              res.data.list[0].content ? res.data.list[0].content : []
            )
          );
        }
      });
    };
  },

  //删除的方法
  delList: (id) => {
    return (dispatch, getState) => {
      reqcartdelete({ id }).then((res) => {
        if (res.data.code == 200) {
          Toast.show({
            icon: "success",
            content: "删除成功",
          });
          dispatch(actions.getList());
        }
      });
    };
  },
  //点击删除按钮
  del: () => {
    return (dispatch, getState) => {
      let {
        shop: { normalList, groupList },
      } = getState();
      normalList.forEach((item) => {
        if (item.checked) {
          dispatch(actions.delList(item.id));
        }
      });
      groupList.forEach((item) => {
        if (item.checked) {
          dispatch(actions.delList(item.id));
        }
      });
    };
  },

  //点击单个复选框按钮
  changeOne: (e, index, item) => {
    return (dispatch, getState) => {
      let {
        shop: { normalList, groupList,allChecked },
      } = getState();
      item.checked = e.target.checked; //不会引起页面重新渲染
      let newArr = [];
      if (item.type == "2") {
        groupList.forEach((newitem, newindex) => {
          if (newindex == index) {
            newArr.push(item);
            return;
          }
          newArr.push(newitem);
        });
        dispatch(actions.changeGroupList(newArr));
      } else {
        normalList.forEach((newitem, newindex) => {
          if (newindex == index) {
            newArr.push(item);
            return;
          }
          newArr.push(newitem);
        });
        dispatch(actions.changeNormalList(newArr));
      }

      //每次点击，判断全选按钮状态
      let tag = false;
      let tag1 = normalList.every((item) => {
        return item.checked;
      });
      console.log("tag1:-------"+tag1);
      let tag2 = groupList.every((item) => {
        return item.checked;
      });
      console.log("tag2:-----"+tag2);
      if (tag1 && tag2) {
        tag = true;
      }
      console.log("tag:--------" + tag);
      dispatch(actions.changeAllChecked(tag),()=>{
          console.log(allChecked);
      });
    };
  },

  //点击+
  add: (id) => {
    return (dispatch, getState) => {
      let token = JSON.parse(sessionStorage.getItem("userInfo")).token;
      let type = 2;
      dispatch(actions.editShop({ id, type, token }));
    };
  },
  //点击-
  sub: (id, item) => {
    return (dispatch, getState) => {
      if (item.num == 1) {
        Toast.show({
          content: "商品不能再少了哦~",
        });
        return;
      }
      let token = JSON.parse(sessionStorage.getItem("userInfo")).token;
      let type = 1;
      dispatch(actions.editShop({ id, type, token }));
    };
  },
  //修改数量请求
  editShop: (data) => {
    return (dispatch, getState) => {
      reqcartedit(data).then((res) => {
        if (res.data.code == 200) {
          Toast.show({
            icon: "success",
            content: "操作成功",
          });
          dispatch(actions.getList());
        }
      });
    };
  },

  //点击全选按钮
  changeAll: (e) => {
    return (dispatch, getState) => {
      let {
        shop: { normalList, groupList },
      } = getState();
      normalList.forEach((item) => {
        item.checked = e.target.checked;
      });
      groupList.forEach((item) => {
        item.checked = e.target.checked;
      });
      dispatch(actions.changeNormalList([...normalList]))
      dispatch(actions.changeGroupList([...groupList]))
      dispatch(actions.changeAllChecked(e.target.checked))
    };
  },
};

//导出数据
export const getNormalList = (state) => state.shop.normalList;
export const getGroupList = (state) => state.shop.groupList;
export const getAllChecked = (state) => state.shop.allChecked;

//导出reducer
export default reducer;
